package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McInfoListMapper;
import cc.rengu.igas.momp.common.dao.McOrgInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.McInfoListMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.McOrgInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.*;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.model.bean.JsonTmpApsMcInfo;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryAgentProfitSharingMcListInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryAgentProfitSharingMcListInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/16 12:26
 * @description 代理商分润类活动列表查询功能实现
 */
public class QueryAgentProfitSharingMcListService extends RadpService {

    /**
     * 法人机构号
     */
    private String globalInstId;
    /**
     * 操作类型
     */
    private String globalQueryType;
    /**
     * 操作人
     */
    private String globalQueryId;
    /**
     * 操作角色
     */
    private String globalQueryRole;
    /**
     * 操作人所属机构
     */
    private String globalQueryOrgId;
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;
    private List<TMMcOrgInfo> tmMcOrgInfoList = new ArrayList<>();
    /*营销活动查询开始时间*/
    private String globalStartTime;
    /*营销活动查询结束时间*/
    private String globalEndTime;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest = new QueryAgentProfitSharingMcListInfoRequest();
            ConvertUtil.convertOutput(queryAgentProfitSharingMcListInfoRequest);

            QueryAgentProfitSharingMcListInfoResponse queryAgentProfitSharingMcListInfoResponse = new QueryAgentProfitSharingMcListInfoResponse();
            queryAgentProfitSharingMcListInfoResponse.setHeader(queryAgentProfitSharingMcListInfoRequest.getHeader());
            BizResponse<QueryAgentProfitSharingMcListInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryAgentProfitSharingMcListInfoResponse);

            //报文检查
            messageValidation(queryAgentProfitSharingMcListInfoRequest);
            //查询上送机构及下属机构
            McOrgInfoMapper mcOrgInfoMapper = new McOrgInfoMapperImpl();
            tmMcOrgInfoList = mcOrgInfoMapper.selectAllMcOrgInfoListByinstIdOrgId(queryAgentProfitSharingMcListInfoRequest.getHeader().getInstId(), globalQueryOrgId);
            if (null == tmMcOrgInfoList || tmMcOrgInfoList.size() == 0) {
                /* 代理商、收银员或用户 */
                rglog.error("查询当前角色<{}>机构<{}>及其下属机构失败!", queryAgentProfitSharingMcListInfoRequest.getQueryRole(), globalQueryOrgId);
                throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
            }
            // 服务调用
            BizResponse<QueryAgentProfitSharingMcListInfoResponse> bizResponseNew = queryAgentProfitSharingMcList(queryAgentProfitSharingMcListInfoRequest, queryAgentProfitSharingMcListInfoResponse, xmlTreeUtil);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }


    /**
     * 代理商营销活动列表查询
     *
     * @author liujinan
     * @date 2020/6/17 16:59
     */
    private BizResponse<QueryAgentProfitSharingMcListInfoResponse> queryAgentProfitSharingMcList(QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest, QueryAgentProfitSharingMcListInfoResponse queryAgentProfitSharingMcListInfoResponse, XmlTreeUtil xmlTreeUtil)
            throws Exception {

        BizResponse<QueryAgentProfitSharingMcListInfoResponse> bizResponse = new BizResponse<>();
        rglog.debug("TXN_NUM = {}", queryAgentProfitSharingMcListInfoRequest.getHeader().getTxnNum());

        List<ApsMcListInfoOutObj> apsMcListInfoOutObjList = new ArrayList<>();

        List<AgentProfitSharingMcListInfoBean> agentProfitSharingMcListInfoBeanList;
        rglog.debug("当前是<{}>查询数据", QueryRoleEnum.getByValue(globalQueryRole).getQueryRoleDesc());
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType) && QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
            //银行审批岗因为数据查询的为业务数据临时表所以单独处理
            agentProfitSharingMcListInfoBeanList = processTmpApsMcListInfo(queryAgentProfitSharingMcListInfoRequest);

            if (null == agentProfitSharingMcListInfoBeanList || agentProfitSharingMcListInfoBeanList.isEmpty()) {
                rglog.info("根据查询条件查询到0条代理商分润类营销活动信息!");
                queryAgentProfitSharingMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryAgentProfitSharingMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                queryAgentProfitSharingMcListInfoResponse.setTotalRows(String.valueOf(agentProfitSharingMcListInfoBeanList.size()));
                if (CommonConstant.ZERO == (agentProfitSharingMcListInfoBeanList.size() % Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize()))) {
                    queryAgentProfitSharingMcListInfoResponse.setTotalPage(String.valueOf(agentProfitSharingMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize()))));
                } else {
                    queryAgentProfitSharingMcListInfoResponse.setTotalPage(String.valueOf(agentProfitSharingMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize())) + 1));
                }

                /* 分页查询 */
                int fromIndex = Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageIndex()) * Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize());
                int endIndex = fromIndex + Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize());
                if (fromIndex <= agentProfitSharingMcListInfoBeanList.size()) {
                    List<AgentProfitSharingMcListInfoBean> agentProfitSharingMcListInfoBeanList1 = agentProfitSharingMcListInfoBeanList.subList(fromIndex, Math.min(endIndex, agentProfitSharingMcListInfoBeanList.size()));
                    queryAgentProfitSharingMcListInfoResponse.setMcListInfo(agentProfitSharingMcListInfoBeanList1);
                }
            }
        } else {
            //查询数据
            PageResult pageResult = getAllAgentProfitSharingMcInfo(queryAgentProfitSharingMcListInfoRequest);
            //根据取出记录条数做对应处理
            if (null == pageResult || null == pageResult.getResult() || null == pageResult.getPageInfo() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条代理商分润营销活动信息!");
                queryAgentProfitSharingMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryAgentProfitSharingMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                for (Object object : pageResult.getResult()) {
                    apsMcListInfoOutObjList.add((ApsMcListInfoOutObj) object);
                }

                rglog.info("记录条数:SIZE=<{}>", pageResult.getResult().size());
                agentProfitSharingMcListInfoBeanList = apsMcListInfoOutObjList.stream().map(this::convertAgentProfitSharingInfo).collect(Collectors.toList());
                if (null != queryAgentProfitSharingMcListInfoRequest.getMcStatus() && queryAgentProfitSharingMcListInfoRequest.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                    agentProfitSharingMcListInfoBeanList = agentProfitSharingMcListInfoBeanList.stream().filter(item -> item.getMcStatus().equals(McStatusEnum.ENDED.getMcStatusCode()) || item.getMcStatus().equals(McStatusEnum.FORCE_ENDED.getMcStatusCode())).collect(Collectors.toList());
                }
                queryAgentProfitSharingMcListInfoResponse.setMcListInfo(agentProfitSharingMcListInfoBeanList);

                queryAgentProfitSharingMcListInfoResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryAgentProfitSharingMcListInfoResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));
            }
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryAgentProfitSharingMcListInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryAgentProfitSharingMcListInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryAgentProfitSharingMcListInfoResponse);

        return bizResponse;
    }

    /**
     * @param queryAgentProfitSharingMcListInfoRequest
     * @author liujinan
     * @date 2020/5/27
     * @description 查询代理商分润类营销活动列表信息
     */
    private PageResult getAllAgentProfitSharingMcInfo(QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest) throws Exception {

        //查询类型
        String queryType = queryAgentProfitSharingMcListInfoRequest.getQueryType();
        //查询主体角色
        String queryRole = queryAgentProfitSharingMcListInfoRequest.getQueryRole();

        /* 数据库入参赋值 */
        ApsMcListInfoInObj apsMcListInfoInObj = setupDatabaseInputObject(queryAgentProfitSharingMcListInfoRequest);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageIndex()));
        pageInfo.setPageSize(Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize()));

        //查询类型为收银员,商户,用户,无权限查询
        if (QueryTypeEnum.CASHIER_QYERY_TYPE.getQueryTypeCode().equals(queryType) ||
                QueryTypeEnum.MCHNT_QYERY_TYPE.getQueryTypeCode().equals(queryType) ||
                QueryTypeEnum.USER_QYERY_TYPE.getQueryTypeCode().equals(queryType)) {
            rglog.error("查询类型为<{}>,无权限查询代理商分润类营销活动列表信息!", queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_QUERY_ERROR.getRespDesc());
        }
        //查询类型为代理商
        else if (QueryTypeEnum.AGENT_QYERY_TYPE.getQueryTypeCode().equals(queryType)) {
            switch (QueryRoleEnum.getByValue(queryRole)) {
                case AGENT_QYERY_ROLE:
                    //代理商
                    return selectApsMcListInfoByAgent(apsMcListInfoInObj, pageInfo);
                default:
                    //其他查询角色不支持
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", queryRole, queryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }
        }
        //查询类型为银行
        else if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(queryType)) {

            switch (QueryRoleEnum.getByValue(queryRole)) {
                case BANK_BASIC_QUERY_ROLE:
                    /* 银行录入岗 */
                    return selectApsMcListInfoByBankBasicRole(apsMcListInfoInObj, pageInfo);
                case BANK_PUBLISH_QYERY_ROLE:
                    /* 银行发布岗 */
                    return selectApsMcListInfoByBankPublishRole(apsMcListInfoInObj, pageInfo);
                default:
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户无权限操作数据!", queryRole);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }
        } else {
            /* 其他查询类型不支持 */
            rglog.error("当前<{}>查询类型的查询用户无权限操作数据!", queryType);
            throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }
    }

    /**
     * 代理商查询数据
     *
     * @param apsMcListInfoInObj 营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/6/18
     */
    private PageResult selectApsMcListInfoByAgent(ApsMcListInfoInObj apsMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectApsMcListInfoByAgent(apsMcListInfoInObj, pageInfo);
    }

    /**
     * 数据库查询前赋值数据库实体类入参
     *
     * @param queryAgentProfitSharingMcListInfoRequest 代理商活动列表查询接口请求对象
     * @return cc.rengu.igas.momp.common.entity.MoneyOffMcListInfoInObj 营销活动列表查询使用实体类入参
     * @author Jinan Liu
     * @date 2020/5/28 12:57
     */
    private ApsMcListInfoInObj setupDatabaseInputObject(QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest) {

        ApsMcListInfoInObj apsMcListInfoInObj = new ApsMcListInfoInObj();

        /* 机构编码 */
        apsMcListInfoInObj.setInstId(queryAgentProfitSharingMcListInfoRequest.getHeader().getInstId());
        rglog.info("INST_ID=<{}>", queryAgentProfitSharingMcListInfoRequest.getHeader().getInstId());
        /* 营销活动编号 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcNo())) {
            apsMcListInfoInObj.setMcNo(queryAgentProfitSharingMcListInfoRequest.getMcNo());
        }
        /* 营销活动名称 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcName())) {
            apsMcListInfoInObj.setMcName(queryAgentProfitSharingMcListInfoRequest.getMcName());
        }
        /* 营销活动类型 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcType())) {
            apsMcListInfoInObj.setMcType(queryAgentProfitSharingMcListInfoRequest.getMcType());
        }
        /* 营销活动状态 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcStatus())) {
            apsMcListInfoInObj.setMcStatus(queryAgentProfitSharingMcListInfoRequest.getMcStatus());
            if (queryAgentProfitSharingMcListInfoRequest.getMcStatus().equals(McStatusEnum.ALL_ENDED.getMcStatusCode())) {
                apsMcListInfoInObj.setRemark1(McStatusEnum.ALL_ENDED.getMcStatusCode());
            }
        }
        /* 审批状态 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getAuditStatus())) {
            apsMcListInfoInObj.setAuditStatus(queryAgentProfitSharingMcListInfoRequest.getAuditStatus());
        }
        /* 代理商编号 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getAgentMchntNo())) {
            apsMcListInfoInObj.setAgentMchntNo(queryAgentProfitSharingMcListInfoRequest.getAgentMchntNo());
        }
        /* 当前系统日期 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getHeader().getTransDate())) {
            apsMcListInfoInObj.setTxnDate(queryAgentProfitSharingMcListInfoRequest.getHeader().getTransDate());
            rglog.info("TXN_DATE=<{}>", queryAgentProfitSharingMcListInfoRequest.getHeader().getTransDate());
        }

        /* 查询主体所属机构 */
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getQueryOrgId())) {
            apsMcListInfoInObj.setOrgId(queryAgentProfitSharingMcListInfoRequest.getQueryOrgId());
        }
        if (!StringUtil.isNullorEmpty(globalStartTime)) {
            apsMcListInfoInObj.setStartTime(globalStartTime);
        }
        if (!StringUtil.isNullorEmpty(globalEndTime)) {
            apsMcListInfoInObj.setEndTime(globalEndTime);
        }
        return apsMcListInfoInObj;
    }


    /**
     * 银行操作员查询代理商分润营销活动列表数据库操作
     *
     * @param apsMcListInfoInObj 营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 00:47
     */
    private PageResult selectApsMcListInfoByBankBasicRole(ApsMcListInfoInObj apsMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectApsMcListInfoByBankBasicRole(apsMcListInfoInObj, pageInfo);
    }

    /**
     * 银行发布员查询代理商营销活动列表数据库操作
     *
     * @param apsMcListInfoInObj 营销活动列表查询使用实体类入参
     * @param pageInfo           数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 01:13
     */
    private PageResult selectApsMcListInfoByBankPublishRole(ApsMcListInfoInObj apsMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectApsMcListInfoByBankPublishRole(apsMcListInfoInObj, pageInfo);
    }

    /**
     * 查询数据库结果转换为接口响应
     *
     * @author liujinan
     * @date 2020/6/18 01:13
     */
    private AgentProfitSharingMcListInfoBean convertAgentProfitSharingInfo(ApsMcListInfoOutObj apsMcListInfoOutObj) {

        AgentProfitSharingMcListInfoBean agentProfitSharingMcListInfoBean = new AgentProfitSharingMcListInfoBean();
        try {
            /* 营销活动编号 */
            agentProfitSharingMcListInfoBean.setMcNo(apsMcListInfoOutObj.getMcNo());
            /* 营销活动名称 */
            agentProfitSharingMcListInfoBean.setMcName(apsMcListInfoOutObj.getMcName());
            /* 营销活动类型 */
            agentProfitSharingMcListInfoBean.setMcType(apsMcListInfoOutObj.getMcType());
            /* 营销活动状态 */
            agentProfitSharingMcListInfoBean.setMcStatus(apsMcListInfoOutObj.getMcStatus());
            /* 活动起始日期 */
            agentProfitSharingMcListInfoBean.setMcStartDate(apsMcListInfoOutObj.getMcStartDate());
            /* 活动截止日期 */
            agentProfitSharingMcListInfoBean.setMcEndDate(apsMcListInfoOutObj.getMcEndDate());
            /* 活动结束日期 */
            agentProfitSharingMcListInfoBean.setMcActualEndDate(apsMcListInfoOutObj.getMcActualEndDate());
            /* 活动预算 */
            agentProfitSharingMcListInfoBean.setBudget(apsMcListInfoOutObj.getBudget());
            /* 已消耗预算 */
            if (StringUtil.isNullorEmpty(apsMcListInfoOutObj.getTotalAmtAcc())) {
                agentProfitSharingMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            } else {
                agentProfitSharingMcListInfoBean.setBudgetUsed(apsMcListInfoOutObj.getTotalAmtAcc());
            }
            /* 审批状态 */
            agentProfitSharingMcListInfoBean.setAuditStatus(apsMcListInfoOutObj.getAuditStatus());
            /* 创建人 */
            agentProfitSharingMcListInfoBean.setOprId(apsMcListInfoOutObj.getOprId());
            /* 创建时间 */
            agentProfitSharingMcListInfoBean.setCreateTime(apsMcListInfoOutObj.getCreateTime());
            /* 最后修改人 */
            if (StringUtil.isNullorEmpty(apsMcListInfoOutObj.getLastOprId())) {
                agentProfitSharingMcListInfoBean.setLastOprId("");
            } else {
                agentProfitSharingMcListInfoBean.setLastOprId(apsMcListInfoOutObj.getLastOprId());
            }
            /* 修改时间 */
            if (StringUtil.isNullorEmpty(apsMcListInfoOutObj.getUpdateTime())) {
                agentProfitSharingMcListInfoBean.setUpdateTime("");
            } else {
                agentProfitSharingMcListInfoBean.setUpdateTime(apsMcListInfoOutObj.getUpdateTime());
            }
            //判断上送机构及其下属机构是否包含活动原创建机构
            List<TMMcOrgInfo> newList = tmMcOrgInfoList.stream().filter(item -> item.getOrgId().equals(apsMcListInfoOutObj.getCrtOrgId())).collect(Collectors.toList());
            if (null != newList && newList.size() > 0) {
                /* 已参与营销活动 */
                agentProfitSharingMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_YES);
            } else {
                /* 未参与营销活动 */
                agentProfitSharingMcListInfoBean.setAttendFlag(CommonConstant.ATTEND_FLAG_NO);
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return agentProfitSharingMcListInfoBean;
    }

    /**
     * 银行审批岗数据查询
     *
     * @param queryAgentProfitSharingMcListInfoRequest 代理商分润活动列表查询接口请求对象
     * @return java.util.List<cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcListInfoBean> 代理商分润类营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/20 16:53
     */
    private List<AgentProfitSharingMcListInfoBean> processTmpApsMcListInfo(QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest) throws Exception {

        List<AgentProfitSharingMcListInfoBean> agentProfitSharingMcListInfoBeanList;

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setInstId(globalInstId);
        tmPlatBizTmp.setOrgId(globalQueryOrgId);
        tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_APS_INFO.getTableNameCode());

        //营销活动编号
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcNo())) {
            tmPlatBizTmp.setMcNo(queryAgentProfitSharingMcListInfoRequest.getMcNo());
        }
        //营销活动状态
        if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcStatus())) {
            tmPlatBizTmp.setMcStatus(queryAgentProfitSharingMcListInfoRequest.getMcStatus());
        }

        // 查询临时表获取数据
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        List<TMPlatBizTmp> tmPlatBizTmpList = platBizTmpMapper.selectTmpMcListInfo(tmPlatBizTmp);
        if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {
            agentProfitSharingMcListInfoBeanList = tmPlatBizTmpList.stream().map(this::convertTmpMcInfo).collect(Collectors.toList());
            rglog.debug("活动查询结果 SIZE=<{}>", agentProfitSharingMcListInfoBeanList.size());

            /* 代码过滤查询条件 */
            //按照营销活动名称筛选
            if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcName())) {
                agentProfitSharingMcListInfoBeanList = agentProfitSharingMcListInfoBeanList.stream().filter(item -> item.getMcName().contains(queryAgentProfitSharingMcListInfoRequest.getMcName())).collect(Collectors.toList());
            }
            //按照营销活动类型筛选
            if (!StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getMcType())) {
                agentProfitSharingMcListInfoBeanList = agentProfitSharingMcListInfoBeanList.stream().filter(item -> item.getMcType().equals(queryAgentProfitSharingMcListInfoRequest.getMcType())).collect(Collectors.toList());
            }
            return agentProfitSharingMcListInfoBeanList;
        } else {
            return null;
        }
    }

    /**
     * 转换换业务数据临时表临时数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.AgentProfitSharingMcListInfoBean 代理商分润类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/20 15:37
     */
    private AgentProfitSharingMcListInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {

        AgentProfitSharingMcListInfoBean agentProfitSharingMcListInfoBean = new AgentProfitSharingMcListInfoBean();
        try {
            String mcNo;
            String mcName;
            String mcType;
            String mcStartDate;
            String mcEndDate;
            String mcActualEndDate;
            String budget;
            /* 根据营销活动类型解析BIZ_DATA1字段内容 */
            String mcTypeOne = tmPlatBizTmp.getMcNo().substring(2, 3);
            if (McTypeEnum.MC_TYPE_APS.getMcTypeCode().equals(mcTypeOne)) {
                JsonTmpApsMcInfo jsonTmpApsMcInfo = JsonOperation.parApsMcInfoTmpBizData1(tmPlatBizTmp, rglog);
                mcNo = jsonTmpApsMcInfo.getMcNo();
                mcName = jsonTmpApsMcInfo.getMcName();
                mcType = jsonTmpApsMcInfo.getMcType();
                mcStartDate = jsonTmpApsMcInfo.getMcStartDate();
                mcEndDate = jsonTmpApsMcInfo.getMcEndDate();
                mcActualEndDate = jsonTmpApsMcInfo.getMcActualEndDate();
                budget = jsonTmpApsMcInfo.getBudget();

            } else {
                rglog.error("不支持的营销活动类型<{}>,不可转换数据!", mcTypeOne);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespDesc());
            }

            /* 营销活动编号 */
            agentProfitSharingMcListInfoBean.setMcNo(mcNo);
            /* 营销活动名称 */
            agentProfitSharingMcListInfoBean.setMcName(mcName);
            /* 营销活动类型 */
            agentProfitSharingMcListInfoBean.setMcType(mcType);
            /* 活动起始日期 */
            agentProfitSharingMcListInfoBean.setMcStartDate(mcStartDate);
            /* 活动截止日期 */
            agentProfitSharingMcListInfoBean.setMcEndDate(mcEndDate);
            /* 活动结束日期 */
            agentProfitSharingMcListInfoBean.setMcActualEndDate(mcActualEndDate);
            /* 活动预算 */
            agentProfitSharingMcListInfoBean.setBudget(budget);
            /* 已消耗预算 */
            agentProfitSharingMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            /* 审批状态 */
            agentProfitSharingMcListInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            agentProfitSharingMcListInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            agentProfitSharingMcListInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            agentProfitSharingMcListInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            agentProfitSharingMcListInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());

        } catch (Exception e) {
            rglog.error("转换营销活动临时表信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return agentProfitSharingMcListInfoBean;
    }

    /**
     * 请求报文体内容检查
     *
     * @param queryAgentProfitSharingMcListInfoRequest 代理商分润类活动列表查询接口请求对象
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryAgentProfitSharingMcListInfoRequest queryAgentProfitSharingMcListInfoRequest) {

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryAgentProfitSharingMcListInfoRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryAgentProfitSharingMcListInfoRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryAgentProfitSharingMcListInfoRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryAgentProfitSharingMcListInfoRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryAgentProfitSharingMcListInfoRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getPageIndex())) {
            rglog.error("当前页数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageIndex = Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageIndex());
            if (CommonConstant.ZERO > pageIndex) {
                rglog.error("当前查询页数必须大于0! PAGE_INDEX=<{}>", pageIndex);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (StringUtil.isNullorEmpty(queryAgentProfitSharingMcListInfoRequest.getPageSize())) {
            rglog.error("页显示记录数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageSize = Integer.parseInt(queryAgentProfitSharingMcListInfoRequest.getPageSize());
            if (CommonConstant.ONE > pageSize) {
                rglog.error("每页记录数必须大于等于1! PAGE_SIZE=<{}>", pageSize);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }
        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
        globalStartTime = queryAgentProfitSharingMcListInfoRequest.getStartTime();
        globalEndTime = queryAgentProfitSharingMcListInfoRequest.getEndTime();
    }
}

